package cc.lucien.dao;

import cc.lucien.utils.DBHelper;

import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
    /**
     * 查询是否有该用户名
     * @param username
     * @return
     */
    public ResultSet findByUsername(String username) {
        String result = "";
        String sql = "select * from users where username = ?";
        return DBHelper.executeQuery(sql,username);
    }

    /**
     * 添加用户
     * @param objects
     * @return
     */
    public int add(Object[] objects) {
        String sql = "insert into users values(?,?,?,?,?,?,?)";
        return DBHelper.executeUpdate(sql, objects);
    }

    /**
     * 根据用户名和密码查询
     * @param username
     * @param password
     * @return
     */
    public ResultSet findByUsernameAndPassword(String username, String password) {
        String sql = "select username,password from users where username = ? and password = ?";
        return DBHelper.executeQuery(sql, username, password);
    }

    /**
     * 查询所有数据
     * @return
     */
    public ResultSet findAll() {
        String sql = "select * from users";
        return DBHelper.executeQuery(sql);
    }

    /**
     * 删除一条记录
     * @param username
     * @return
     */
    public int deleteByUsername(String username) {
        String sql = "delete from users where username = ?";
        return DBHelper.executeUpdate(sql, username);
    }


    /**
     * 修改数据
     * @param
     * @return
     */
    public int update(String username, String password, String realname, String imgurl, String email, Long phone, String source) {
        String sql = "update users set realname=?,imgurl=?,email=?,phone=?,source=?" +
                "where username=? and password=?";
        return DBHelper.executeUpdate(sql, realname,imgurl,email,phone,source,username,password);
    }

}
